<?php

/**
 * This is the model class for table "comment".
 *
 * The followings are the available columns in table 'comment':
 * @property integer $id
 * @property integer $music_id
 * @property integer $list_id
 * @property integer $user_id
 * @property string $posttime
 * @property string $content
 * @property integer $like
 * @property string $other
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Listmusic $list
 * @property Music $music
 */
class Comment extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'comment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, posttime, content', 'required'),
			array('music_id, list_id, user_id, like', 'numerical', 'integerOnly'=>true),
			array('other', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, music_id, list_id, user_id, posttime, content, like, other', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'list' => array(self::BELONGS_TO, 'Listmusic', 'list_id'),
			'music' => array(self::BELONGS_TO, 'Music', 'music_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'music_id' => 'Music',
			'list_id' => 'List',
			'user_id' => 'User',
			'posttime' => 'Posttime',
			'content' => 'Content',
			'like' => 'Like',
			'other' => 'Other',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('music_id',$this->music_id);
		$criteria->compare('list_id',$this->list_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('posttime',$this->posttime,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('like',$this->like);
		$criteria->compare('other',$this->other,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Comment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return boolean add comment. Rerun false add comment false
	 */
	public function add()
	{
		$this->attributes=$_POST['Comment'];
		return $this->save();
	}

	/**
	 * @param int $id id comment.
	 * @return array have  content comment for id comment
	 */
	public function view($comment_id)
	{
		return $this->findByPk($comment_id)->attributes;
	}

	public function edit($comment_id)
	{
		$ob = $this->findByPk($comment_id);
		$ob->attributes=$_POST['Comment'];
		return $ob->save();
	}

	/**
	 * Remove comment by id
	 * @param int $id id comment
	 */
	public function remove($comment_id)
	{
		return $this->deleteByPk($comment_id);
	}

	public function viewComment($comment_id,$type)
	{
		$Criteria = new CDbCriteria();
		switch ($type) {
			case 'list':
				$Criteria->condition = "list_id = $comment_id";
				break;
			case 'music':
				$Criteria->condition = "music_id = $comment_id";
				break;
		}
		$data = $this->findAll($Criteria);
		return $data;
	}

	public function getLinkByIdComment($comment_id)
	{
		$sql="
			SELECT
				music_id,list_id
			FROM
				`comment`
			WHERE
				`comment`.id = $comment_id";
		$query=Yii::app()->db->createCommand($sql)->queryRow();
		return $query;
	}
}
